﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.NativeInterop;
using System.IO;


namespace Game
{
    public partial class Form1 : Form
    {
        IniFile Gearini = new IniFile(Application.StartupPath + "/Gear.ini");
        IniFile ConfigIni = new IniFile(Application.StartupPath + "/Config.ini");
        Random rand = new Random();
        public static int NOA;
        public static int picLoc = 0;
        Image[] ImaArray;
        public static int MaxInv;
        public static int MaxSp;
        
        public Form1()
        {
            NOA = ConfigIni.ReadInt("Avatars", "Avatars", 0);
            MaxInv = ConfigIni.ReadInt("Game", "Inventory", 0);
            MaxSp = ConfigIni.ReadInt("Game", "Spells", 0);
            ImaArray = new Image[NOA]; 
            for (int i = 0; i < NOA; i++)
            {
                ImaArray[i] = Image.FromFile( (i + 1) + ".png");
            }
            InitializeComponent();
            ChangePan("Loading Screen");

            this.pbox_Logo.Image = Image.FromFile("Logo.png");
            this.pbox_NewGame.Image = Image.FromFile("NewGame.png");
            this.pbox_LoadGame.Image = Image.FromFile("LoadGame.png");
            this.pbox_NewAvatarLeft.Image = Image.FromFile("LeftArrow.png");
            this.pbox_NewAvatarRight.Image = Image.FromFile("RightArrow.png");
            this.pbox_NewAvatar.Image = ImaArray[picLoc];
            this.ClientSize = new System.Drawing.Size(600, 400);
        }

        #region Load Game Button
        public void pbox_LoadGame_Click(object sender, EventArgs e)
        {
            ChangePan("Load Game");
        } 
        #endregion
        #region New Game Button
        public void pbox_NewGame_Click(object sender, EventArgs e)
        {
            ChangePan("New Game");
        }
        #endregion

        private void pbox_NewAvatarRight_Click(object sender, EventArgs e)
        {
            if (picLoc < (NOA - 1) )
            {
                picLoc++;
                this.pbox_NewAvatar.Image = ImaArray[picLoc];
            }
            else
            {
                picLoc = 0;
                this.pbox_NewAvatar.Image = ImaArray[picLoc];
            }
        }

        private void pbox_NewAvatarLeft_Click(object sender, EventArgs e)
        {
            if (picLoc > 0)
            {
                picLoc--;
                this.pbox_NewAvatar.Image = ImaArray[picLoc];
            }
            else
            {
                picLoc = (NOA - 1);
                this.pbox_NewAvatar.Image = ImaArray[picLoc];
            }
        }

        public void btn_StartGame_Click(object sender, EventArgs e)
        {
            string Name = txt_NewName.Text;
            string Class = cbox_NewClass.Text;
            string Level = cbox_NewGameLevel.Text;
            string Avatar = (picLoc + 1) + ".png";
            if (Name == "" || Class == "" || Level == "")
            {
                if (Name == "")
                {
                    MessageBox.Show("Please Enter a Name");
                }
                if (Class == "")
                {
                    cbox_NewClass.SelectedIndex = 0;
                }
                if (Level == "")
                {
                    cbox_NewGameLevel.SelectedIndex = 0;
                }
            }
            else
            {
                IniFile SaveIni = new IniFile(Application.StartupPath + "/" + Name + ".Sav");
                SaveIni.WriteString("Character", "Name", Name);
                SaveIni.WriteString("Character", "Class", Class); 
                SaveIni.WriteString("Character", "Level", 1);
                SaveIni.WriteString("Character", "Money", 1000);
                SaveIni.WriteString("Character", "LocationX", 1);
                SaveIni.WriteString("Character", "LocationY", 1);
                SaveIni.WriteString("User", "Avatar", Avatar);
                SaveIni.WriteString("User", "GameLevel", Level);
                SaveIni.WriteString("Inventory", "Inventory", 0);
                SaveIni.WriteString("Spells", "Spells", 0);
                SaveIni.WriteString("Equipment", "Equipment", 0);
                #region Inventory, Spells and Equipment
                for (int i = 0; i < MaxInv; i++)
                {
                    SaveIni.WriteString("Inventory", "Item" + i, 0);
                }                
                for (int i = 0; i < MaxSp; i++)
                {
                    SaveIni.WriteString("Spells", "Spell" + i, 0);
                }                
                for (int i = 0; i < 10; i++)
                {
                    SaveIni.WriteString("Equipment", "Equip" + i, 0);
                }
                #endregion
                #region Attack, M Attack, Defence, M Defence, HP of Each Class
                if (Class == "Assassin")
                {
                    SaveIni.WriteString("Character", "Attack", 25);
                    SaveIni.WriteString("Character", "MAttack", 10);
                    SaveIni.WriteString("Character", "Defence", 15);
                    SaveIni.WriteString("Character", "MDefence", 20);
                    SaveIni.WriteString("Character", "HP", 5);
                }
                else if (Class == "Knight")
                {
                    SaveIni.WriteString("Character", "Attack", 20);
                    SaveIni.WriteString("Character", "MAttack", 15);
                    SaveIni.WriteString("Character", "Defence", 25);
                    SaveIni.WriteString("Character", "MDefence", 5);
                    SaveIni.WriteString("Character", "HP", 10);
                }
                else if (Class == "Mage")
                {
                    SaveIni.WriteString("Character", "Attack", 15);
                    SaveIni.WriteString("Character", "MAttack", 25);
                    SaveIni.WriteString("Character", "Defence", 5);
                    SaveIni.WriteString("Character", "MDefence", 10);
                    SaveIni.WriteString("Character", "HP", 20);
                }
                else if (Class == "Monk")
                {
                    SaveIni.WriteString("Character", "Attack", 5);
                    SaveIni.WriteString("Character", "MAttack", 20);
                    SaveIni.WriteString("Character", "Defence", 10);
                    SaveIni.WriteString("Character", "MDefence", 15);
                    SaveIni.WriteString("Character", "HP", 25);
                }
                else if (Class == "Slayer")
                {
                    SaveIni.WriteString("Character", "Attack", 10);
                    SaveIni.WriteString("Character", "MAttack", 5);
                    SaveIni.WriteString("Character", "Defence", 20);
                    SaveIni.WriteString("Character", "MDefence", 25);
                    SaveIni.WriteString("Character", "HP", 15);
                }
                #endregion
                ChangePan("Start Game");

            }
        }

        private void btn_Load_Click(object sender, EventArgs e)
        {
            try
            {
                Charr.LoadChar("/" + txt_LoadCharr.Text + ".Sav");
                lbl_WrongLoad.Hide();
                ChangePan("Start Game");
                
            }
            catch 
            {
                lbl_WrongLoad.Show();
            }
        }

        public void ChangePan(string PanName)
        {
            if (PanName == "None" || PanName == "!" || PanName == "")
            {
                #region None
                pan_LoadingScreen.Hide();
                pan_LoadingScreen.Enabled = false;
                //pan_LoadingScreen.Size = new Size(600, 400);
                //pan_LoadingScreen.Location = new Point(0, 0);

                pan_Load.Hide();
                pan_Load.Enabled = false;
                //pan_Load.Size = new Size(600, 400);
                //pan_Load.Location = new Point(0, 0);

                pan_MoveArea.Hide();
                pan_MoveArea.Enabled = false;
                //pan_MoveArea.Size = new Size(400, 400);
                //pan_MoveArea.Location = new Point(0, 0);

                pan_NewGame.Hide();
                pan_NewGame.Enabled = false;
                //pan_NewGame.Size = new Size(600, 400);
                //pan_NewGame.Location = new Point(0, 0);

                pan_Sidebar.Hide();
                pan_Sidebar.Enabled = false;
                //pan_Sidebar.Size = new Size(200, 400);
                //pan_Sidebar.Location = new Point(400, 0);

                pan_StartScreen.Hide();
                pan_StartScreen.Enabled = false;
                //pan_StartScreen.Size = new Size(600, 400);                
                //pan_StartScreen.Location = new Point(0, 0);
                #endregion
            }
            else if (PanName == "New Game")
            {
                #region New Game
                pan_LoadingScreen.Hide();
                pan_LoadingScreen.Enabled = false;
                //pan_LoadingScreen.Size = new Size(600, 400);
                //pan_LoadingScreen.Location = new Point(0, 0);

                pan_Load.Hide();
                pan_Load.Enabled = false;
                //pan_Load.Size = new Size(600, 400);
                //pan_Load.Location = new Point(0, 0);

                pan_MoveArea.Hide();
                pan_MoveArea.Enabled = false;
                //pan_MoveArea.Size = new Size(400, 400);
                //pan_MoveArea.Location = new Point(0, 0);

                pan_NewGame.Show();
                pan_NewGame.Enabled = true;
                pan_NewGame.Size = new Size(600, 400);
                pan_NewGame.Location = new Point(0, 0);

                pan_Sidebar.Hide();
                pan_Sidebar.Enabled = false;
                //pan_Sidebar.Size = new Size(200, 400);
                //pan_Sidebar.Location = new Point(400, 0);

                pan_StartScreen.Hide();
                pan_StartScreen.Enabled = false;
                //pan_StartScreen.Size = new Size(600, 400);                
                //pan_StartScreen.Location = new Point(0, 0);
                #endregion
            }
            else if (PanName == "Load Game")
            {
                #region Load Game
                pan_LoadingScreen.Hide();
                pan_LoadingScreen.Enabled = false;
                //pan_LoadingScreen.Size = new Size(600, 400);
                //pan_LoadingScreen.Location = new Point(0, 0);

                pan_Load.Show();
                pan_Load.Enabled = true;
                pan_Load.Size = new Size(600, 400);
                pan_Load.Location = new Point(0, 0);

                pan_MoveArea.Hide();
                pan_MoveArea.Enabled = false;
                //pan_MoveArea.Size = new Size(400, 400);
                //pan_MoveArea.Location = new Point(0, 0);

                pan_NewGame.Hide();
                pan_NewGame.Enabled = false;
                //pan_NewGame.Size = new Size(600, 400);
                //pan_NewGame.Location = new Point(0, 0);

                pan_Sidebar.Hide();
                pan_Sidebar.Enabled = false;
                //pan_Sidebar.Size = new Size(200, 400);
                //pan_Sidebar.Location = new Point(400, 0);

                pan_StartScreen.Hide();
                pan_StartScreen.Enabled = false;
                //pan_StartScreen.Size = new Size(600, 400);                
                //pan_StartScreen.Location = new Point(0, 0);
                #endregion
            }
            else if (PanName == "Start Screen")
            {
                #region StartScreen
                pan_LoadingScreen.Hide();
                pan_LoadingScreen.Enabled = false;
                //pan_LoadingScreen.Size = new Size(600, 400);
                //pan_LoadingScreen.Location = new Point(0, 0);

                pan_Load.Hide();
                pan_Load.Enabled = false;
                //pan_Load.Size = new Size(600, 400);
                //pan_Load.Location = new Point(0, 0);

                pan_MoveArea.Hide();
                pan_MoveArea.Enabled = false;
                //pan_MoveArea.Size = new Size(400, 400);
                //pan_MoveArea.Location = new Point(0, 0);

                pan_NewGame.Hide();
                pan_NewGame.Enabled = false;
                //pan_NewGame.Size = new Size(600, 400);
                //pan_NewGame.Location = new Point(0, 0);

                pan_Sidebar.Hide();
                pan_Sidebar.Enabled = false;
                //pan_Sidebar.Size = new Size(200, 400);
                //pan_Sidebar.Location = new Point(400, 0);

                pan_StartScreen.Show();
                pan_StartScreen.Enabled = true;
                pan_StartScreen.Size = new Size(600, 400);                
                pan_StartScreen.Location = new Point(0, 0);
                #endregion
            }
            else if (PanName == "Start Game")
            {
                #region Start Game
                pan_LoadingScreen.Hide();
                pan_LoadingScreen.Enabled = false;
                //pan_LoadingScreen.Size = new Size(600, 400);
                //pan_LoadingScreen.Location = new Point(0, 0);

                pan_Load.Hide();
                pan_Load.Enabled = false;
                //pan_Load.Size = new Size(600, 400);
                //pan_Load.Location = new Point(0, 0);

                pan_MoveArea.Show();
                pan_MoveArea.Enabled = true;
                pan_MoveArea.Size = new Size(400, 400);
                pan_MoveArea.Location = new Point(0, 0);

                pan_NewGame.Hide();
                pan_NewGame.Enabled = false;
                //pan_NewGame.Size = new Size(600, 400);
                //pan_NewGame.Location = new Point(0, 0);

                pan_Sidebar.Show();
                pan_Sidebar.Enabled = true;
                pan_Sidebar.Size = new Size(200, 400);
                pan_Sidebar.Location = new Point(400, 0);

                pan_StartScreen.Hide();
                pan_StartScreen.Enabled = false;
                //pan_StartScreen.Size = new Size(600, 400);                
                //pan_StartScreen.Location = new Point(0, 0);
                #endregion
            }
            else if (PanName == "Loading Screen")
            {
                #region Loading Screen
                pan_LoadingScreen.Show();
                pan_LoadingScreen.Enabled = true;
                pan_LoadingScreen.Size = new Size(600, 400);
                pan_LoadingScreen.Location = new Point(0, 0);

                pan_Load.Hide();
                pan_Load.Enabled = false;
                //pan_Load.Size = new Size(600, 400);
                //pan_Load.Location = new Point(0, 0);

                pan_MoveArea.Hide();
                pan_MoveArea.Enabled = false;
                //pan_MoveArea.Size = new Size(400, 400);
                //pan_MoveArea.Location = new Point(0, 0);

                pan_NewGame.Hide();
                pan_NewGame.Enabled = false;
                //pan_NewGame.Size = new Size(600, 400);
                //pan_NewGame.Location = new Point(0, 0);

                pan_Sidebar.Hide();
                pan_Sidebar.Enabled = false;
                //pan_Sidebar.Size = new Size(200, 400);
                //pan_Sidebar.Location = new Point(400, 0);

                pan_StartScreen.Hide();
                pan_StartScreen.Enabled = false;
                //pan_StartScreen.Size = new Size(600, 400);                
                //pan_StartScreen.Location = new Point(0, 0);
                #endregion
            }
        }

        private void LoadTimer_Tick(object sender, EventArgs e)
        {

        }
        public void LoadGear(string FilePath)
        {
        }
    }
}
